package com.android.myapplication.view.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class RecycleAdapterDemo extends RecyclerView.Adapter<RecycleAdapterDemo.MyViewHolder>{
    private Context context;

    private View view;

    List<StudentBean> mList = new ArrayList<>();
    public RecycleAdapterDemo(Context context){
        this.context = context;
    }

    public RecycleAdapterDemo(Context mContext, List<StudentBean> mList) {
        this.context = mContext;
        this.mList = mList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        view = LayoutInflater.from(context).inflate(R.layout.list_item_student,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //将数据和控件绑定
        StudentBean studentBean = mList.get(position);
        holder.setData(mList.get(position));
    }

    @Override
    public int getItemCount() {
        //返回Item总条数
        return mList.size();
    }

    //内部类，绑定控件
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mTxName;
        TextView mTxClassName;
        TextView mTxSex;
        TextView mTxAge;
        ImageView mImgView;
        public MyViewHolder(View view){
            super(view);
            mTxName = view.findViewById(R.id.txt_name);
            mTxClassName = view.findViewById(R.id.txt_class);
            mTxSex = view.findViewById(R.id.txt_sex);
            mTxAge = view.findViewById(R.id.txt_age);
            mImgView = view.findViewById(R.id.image1);
        }

        public void setData(StudentBean bean){
            mTxName.setText(bean.name);
            mTxClassName.setText(bean.className);
            mTxSex.setText(bean.sex == 0 ? "男" : "女");
            mTxAge.setText(bean.age + "岁");
            mImgView.setImageURI(Uri.parse(bean.img));
        }
    }
}